//
//  SoundWaveView.m
//  SoundWave
//
//  Created by 朱帅 on 2017/3/6.
//  Copyright © 2017年 Zs. All rights reserved.
//

#import "SoundWaveView.h"

@interface SoundWaveView ()
//相位变化,用来移动曲线
@property (nonatomic, assign) CGFloat offX;
//存放线的数组
@property (nonatomic, strong) NSMutableArray * linesArr;
//高度
@property (nonatomic, assign) CGFloat maxHeight;
//宽度
@property (nonatomic, assign) CGFloat maxWidth;
//最大振幅
@property (nonatomic, assign) CGFloat maxAmplitude;
//振幅系数
@property (nonatomic, assign) CGFloat amplitudeLevel;

@end

@implementation SoundWaveView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.linesArr = [[NSMutableArray alloc]init];
    self.maxHeight = CGRectGetHeight(self.bounds);
    self.maxWidth = CGRectGetWidth(self.bounds);
    self.maxAmplitude = self.maxHeight - 2;
    self.amplitudeLevel = 1;
}

- (void)setLevelCallback:(void (^)(SoundWaveView *waveView))levelCallback
{
    _levelCallback = levelCallback;
    [self.displayLink invalidate];
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(invokeLevelCallback)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    for(int i=0; i < 5; i++){
        CAShapeLayer *line = [CAShapeLayer layer];
        line.fillColor = [[UIColor clearColor] CGColor];
        line.lineCap = kCALineCapSquare;
        line.lineJoin = kCALineJoinRound;
        line.lineWidth = i == 0 ? 2 : 1;
        CGFloat progress = 1.0f - (CGFloat)i / 5;
        UIColor *color = [[UIColor whiteColor]colorWithAlphaComponent:(i == 0 ? 1.0 : progress *progress)];
        line.strokeColor = color.CGColor;
        [self.layer addSublayer:line];
        [self.linesArr addObject:line];
    }
}

- (void)invokeLevelCallback
{
    self.levelCallback(self);
}

- (void)setLevel:(CGFloat)level
{
    _level = level;
    self.offX -= 0.25f;
    self.amplitudeLevel = fmax(level, 0.01f);
    [self refreshLines];
}

- (void)refreshLines
{
    UIGraphicsBeginImageContext(self.frame.size);
    for(int i=0; i < 5; i++) {
        UIBezierPath *wavelinePath = [UIBezierPath bezierPath];
        CGFloat progress = 1.0f - (CGFloat)i / 5;
        CGFloat nowAmplitudeLevel = (1.5f * progress - 0.5f) * self.amplitudeLevel;
        for(CGFloat x = 0; x < self.maxWidth; x ++) {
            CGFloat midScale = 1 - pow(x / (self.maxWidth / 2)  - 1, 2);
            CGFloat y = midScale * self.maxAmplitude * nowAmplitudeLevel * sinf(2 * M_PI *(x / self.maxWidth) * 1 + self.offX) + (self.maxHeight * 0.5);
            if (x==0) {
                [wavelinePath moveToPoint:CGPointMake(x, y)];
            }
            else {
                [wavelinePath addLineToPoint:CGPointMake(x, y)];
            }
        }
        CAShapeLayer *waveline = [self.linesArr objectAtIndex:i];
        waveline.path = [wavelinePath CGPath];
    }
    UIGraphicsEndImageContext();
}

- (void)dealloc
{

}

@end
